package cn.iocoder.yudao.module.nmkj.controller.admin.introduction;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nmkj.controller.admin.introduction.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.introduction.IntroductionDO;
import cn.iocoder.yudao.module.nmkj.service.introduction.IntroductionService;

@Tag(name = "管理后台 - 商品介绍")
@RestController
@RequestMapping("/nmkj/introduction")
@Validated
public class IntroductionController {

    @Resource
    private IntroductionService introductionService;

    @PostMapping("/create")
    @Operation(summary = "创建商品介绍")
    @PreAuthorize("@ss.hasPermission('nmkj:introduction:create')")
    public CommonResult<Long> createIntroduction(@Valid @RequestBody IntroductionSaveReqVO createReqVO) {
        return success(introductionService.createIntroduction(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品介绍")
    @PreAuthorize("@ss.hasPermission('nmkj:introduction:update')")
    public CommonResult<Boolean> updateIntroduction(@Valid @RequestBody IntroductionSaveReqVO updateReqVO) {
        introductionService.updateIntroduction(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品介绍")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nmkj:introduction:delete')")
    public CommonResult<Boolean> deleteIntroduction(@RequestParam("id") Long id) {
        introductionService.deleteIntroduction(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品介绍")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nmkj:introduction:query')")
    public CommonResult<IntroductionRespVO> getIntroduction(@RequestParam("id") Long id) {
        IntroductionDO introduction = introductionService.getIntroduction(id);
        return success(BeanUtils.toBean(introduction, IntroductionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品介绍分页")
    @PreAuthorize("@ss.hasPermission('nmkj:introduction:query')")
    public CommonResult<PageResult<IntroductionRespVO>> getIntroductionPage(@Valid IntroductionPageReqVO pageReqVO) {
        PageResult<IntroductionDO> pageResult = introductionService.getIntroductionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IntroductionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品介绍 Excel")
    @PreAuthorize("@ss.hasPermission('nmkj:introduction:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIntroductionExcel(@Valid IntroductionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IntroductionDO> list = introductionService.getIntroductionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品介绍.xls", "数据", IntroductionRespVO.class,
                        BeanUtils.toBean(list, IntroductionRespVO.class));
    }

}